/**
 * ConstantDistribution : Definition de la loi comstante
 *
 * @author maria ladoue
 * @version 0.1, 04/07/02 
 */
package br.unb.statistic.distribution;

import java.util.HashMap;
import br.unb.statistic.InterPacketTimeGenerator;
import br.unb.statistic.RandomGenerator;
import br.unb.statistic.inference.Inference;
import br.unb.utils.Resource;

public class ConstantDistribution extends GenericContinuousDistribution implements RandomGenerator, InterPacketTimeGenerator {

    /**
     * Equation de la densite: 
     *    f(x)= a;
     */
    private static int A = 0;

    public ConstantDistribution() {
        paramsCount = 1;
        name = new String("Constant");

        params = new double[paramsCount]; // initialisation du tableau des valeurs des parametres
        paramNames = new String[paramsCount]; // initialisation du tableau des noms des parametres
        params[A] = 1d;
        paramNames[A] = new String("a");
    }

    public ConstantDistribution(double a) {
        this();
        setParam(A, a);
    }

    public boolean hasValidParam(double[] desParams) {
        return true; // TODO: no constraint on A value ?
    }

// Definitions de quelques methodes
    /**
     * Returns the value of the probability distribution function at x
     */
    public double pdf(double x) {
        return params[A];
    }

    /**
     * Returns the value of the cumulative distribution function at x (=Fonction de repartition)
     */
    public double cdf(double x) {
        if (x > 0) {
            return 1d;
        } else {
            return 0d;
        }
    }

    /**
     * Return the value of Inverse Function at x 
     * @param x with x>=0 and x<=1
     */
    public double inverseCdf(double x) throws IllegalArgumentException {
        if (x >= 0 && x <= 1) {
            return 1 / getParam(A);
        } else {
            throw new IllegalArgumentException(Resource.getString("CONSTANTDISTRIBUTION.INVERSECDF_X_MUST_BE_>_0_AND_<_1"));
        }
    }

    public double rand() {
        return 1 / getParam(A);
    }

    public double nextInterPacketTime() {
        return rand();
    }

    public double mean() {
        return params[A];
    }

    public double var() {
        return 0d;
    }

    public String getEquation() {
        return new String("f(x)= a");
    }

    public String xml() {
        String xml = "<CONSTANT>" +
                "<MEAN>" + params[A] + "</MEANS>" +
                "</CONSTANT>";
        return xml;
    }

    public void setParameters(HashMap parameters) {
        if (parameters.get("mean") != null) {
            setParam(A, 1 / java.lang.Double.parseDouble((String) parameters.get("mean")));
        } else {
            setParam(A, java.lang.Double.parseDouble((String) parameters.get("rate")));
        }
    }

    public void estimateParameters(Inference inf) {
        setParam(A, 1 / inf.getMean());
    }
    /* //Old code with the testing unit and stuff YO!!!
    public static void main(String args[]){
    ConstantDistribution dist = new ConstantDistribution(3.5);
    ConstantDistribution dist2 = new ConstantDistribution();
    Log log = new Log("teste",false);
    for(int i=0; i<3000; i++){
    log.write(""+dist.rand());
    }
    log.close();
    try {
    DataReader dr = new DataReader(log.getFile());
    Inference inf = new Inference(dr,0);
    dist2.calculateMLE(inf);
    System.out.println(dist2.getParam(0));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }*/
}
